package com.systop.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.systop.entity.Classify;
import com.systop.service.ClassifyService;
import com.systop.service.OSSFileManage;
import com.systop.utils.FileUploadResult;
import com.systop.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/DataManage")
public class ClassifyController {
    @Autowired
    private ClassifyService classifyService;
    @Autowired
    private OSSFileManage ossFileManage;


    /**
     *
     *  分类界面接口
     *
     * */
    //查询所有类型
    @RequestMapping(value = "/classifyFindAll",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> classifyFindAll(Classify classify){
        System.out.println(classify);
        Map<String,Object> map = new HashMap<String,Object>();
        //返回符合条件数据总数
        int pageCount = classifyService.classifyCountAll(classify);
        //查询数据
        classify.setStart((classify.getStart()-1)*classify.getPageSize());
        List list = classifyService.classifyFindAll(classify);
        //使用工具类进行list空值判断
        if(CollectionUtils.isEmpty(list) && classify.getStart()>0){
            //设置当本页没有数据时返回上一页
            // classify.setStart(classify.getStart()/classify.getPageSize());
            //设置当本页没有数据时返回第一页
            classify.setStart(1);
            return classifyFindAll(classify);
        }
        map.put("pageCount",pageCount);
        map.put("classifyInfo",list);
        return map;
    };

    //前端查询所有分类
    @RequestMapping(value = "/findAllClassify",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> findAllClassify(){
        Map<String,Object> map = new HashMap<String,Object>();
        List<Classify> classifyList = classifyService.findAllClassify();
        map.put("classifyList",classifyList);
        return map;
    }

    //根据id删除指定对象方法
    @RequestMapping(value = "/delClassify",method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String,Object> delUser(Classify classify){
        //定义键值对返回数据
        Map<String,Object> map = new HashMap<String,Object>();
        // int amount = classifyService.findShowCount(classify);
        // int addtype = classify.getClassifyType();
        // int addstate = classify.getIndexShow();
        // //判断显示数据是否符合规格
        // if(addtype==0 && amount<=1 && addstate==1){
        //     map.put("msg","添加失败,类型显示数量必须大于等于1个");
        //     map.put("delSuccNum",0);
        //     return map;
        // }
        // objectName = objectName.replace("https://hello-oss-file.oss-cn-beijing.aliyuncs.com/","");

        //删除前先查询商品中是否包含该分类的商品
        int exist = classifyService.findGoodsByClassifyId(classify);
        System.out.println(exist);
        map.put("exist",exist);
        if(exist==0){
            System.out.println("进入删除方法");
            //如果存在图片就先删除OSS中上传的图片
            if(classify.getClassifyImg() !=null || classify.getClassifyImg().length()>0){
                String url = classify.getClassifyImg();
                System.out.println(url);
                url = url.replace("https://oss-container.oss-cn-beijing.aliyuncs.com/","");
                System.out.println(url);
                System.out.println(url.indexOf("?"));
                //判断路径中是否存在?
                if(url.indexOf("?")!=-1){
                    url = url.substring(0,url.indexOf("?"));
                    System.out.println(url);
                    FileUploadResult result = ossFileManage.delete(url);
                }
            }
            int i = classifyService.delClassify(classify);
            map.put("delSuccNum",i);
            if(i>0){
                map.put("msg", "删除成功!");
            }else{
                map.put("msg","删除失败,用户不存在");
            }
        }else{
            map.put("msg","该分类存在商品,请先删除相关分类商品后,再删除分类");
        }
        return map;
    }

    // //添加数据方法
    // @RequestMapping(value = "/addClassify",method = RequestMethod.POST)
    // @ResponseBody
    // public Map<String,Object> addClassify(@RequestBody Classify classify,MultipartFile file){
    //     System.out.println(classify);
    //     Map<String,Object> map = new HashMap<>();
    //     int exist = classifyService.findClassifyByclassifyName(classify);
    //     map.put("exist", exist);
    //     if(exist == 0){
    //         int i = 0;
    //         int amount = classifyService.findShowCount(classify);
    //         int addtype = classify.getClassifyType();
    //         int addstate = classify.getIndexShow();
    //         map.put("addSuccNum",i);
    //         //判断显示数据是否符合规格
    //         if(addtype==0 && amount>=3 && addstate==1){
    //             map.put("msg","添加失败,类型显示数量最多添加3个");
    //             return map;
    //         // }else if(addtype==0 && amount<1){
    //         //     map.put("msg","添加失败,类型显示数量必须大于等于1个");
    //         //     return map;
    //         }else if(addtype==1 && amount>=1 && addstate==1){
    //             map.put("msg","添加失败,文章显示数量最多添加1个");
    //             return map;
    //         }
    //         // FileUploadResult result = ossFileManage.upload(file);
    //         i = classifyService.addClassify(classify);
    //         map.put("addSuccNum",i);
    //         if(i>0){
    //             map.put("msg", "添加成功!");
    //         }else{
    //             map.put("msg","添加失败,数据错误");
    //         }
    //     }else{
    //         map.put("msg","添加失败,类型已存在");
    //     }
    //     return map;
    // }

    // // 更新数据方法
    // @RequestMapping(value = "/udpClassify",method = RequestMethod.POST)
    // @ResponseBody
    // public Map<String,Object> udpClassify(@RequestBody Classify classify){
    //     Map<String,Object> map = new HashMap<>();
    //     int exist = classifyService.findDataUnlessId(classify);
    //     map.put("exist", exist);
    //     if(exist == 0){
    //         int i = 0;
    //         int amount = classifyService.findShowCount(classify);
    //         int addtype = classify.getClassifyType();
    //         System.out.println(addtype);
    //         int addstate = classify.getIndexShow();
    //         System.out.println(addstate);
    //         map.put("udpSuccNum",i);
    //         //判断显示数据是否符合规格
    //         if(addtype==0 && amount>=3 && addstate==1){
    //             map.put("msg","修改失败,类型显示数量最多添加3个");
    //             return map;
    //         }else if(addtype==0 && amount<1 && addstate==1){
    //             map.put("msg","修改失败,类型显示数量最多添加1个");
    //             return map;
    //         }else if(addtype==1 && amount>=1 && addstate==1){
    //             map.put("msg","修改失败,文章显示数量最多添加1个");
    //             return map;
    //         }
    //         i = classifyService.udpClassify(classify);
    //         map.put("udpSuccNum",i);
    //         if(i>0){
    //             map.put("msg", "修改成功!");
    //         }else{
    //             map.put("msg","修改失败,类型不存在");
    //         }
    //     }else{
    //         map.put("msg","修改失败,类型已存在,或有类似类型");
    //     }
    //     return map;
    // }

    //添加数据方法
    @RequestMapping(value = "/addClassify",method =RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addClassify(@RequestParam("file") MultipartFile file,
                             @RequestParam("classify") String classify){
        System.out.println(classify);
        //将JSON数据转换成java能识别的对象类数据
        Classify classifyPojo =
                JSON.parseObject(JSON.parse(JSON.toJSONString(classify)).toString(),
                        new TypeReference<Classify>(){});
        Map<String,Object> map = new HashMap<>();
        int exist = classifyService.findClassifyByclassifyName(classifyPojo);
        map.put("exist", exist);
        if(exist == 0){
            int i = 0;
            int amount = classifyService.findShowCount(classifyPojo);
            int addtype = classifyPojo.getClassifyType();
            int addstate = classifyPojo.getIndexShow();
            map.put("addSuccNum",i);
            //判断显示数据是否符合规格
            if(addtype==0 && amount>=3 && addstate==1){
                map.put("msg","添加失败,类型显示数量最多添加3个");
                return map;
            // }else if(addtype==0 && amount<1){
            //     map.put("msg","添加失败,类型显示数量必须大于等于1个");
            //     return map;
            }else if(addtype==1 && amount>=1 && addstate==1){
                map.put("msg","添加失败,文章显示数量最多添加1个");
                return map;
            }
            //上传图片到OSS服务器上
            FileUploadResult result = ossFileManage.upload(file);
            if(result.getStatus()=="done"){
                classifyPojo.setClassifyImg(result.getName());
                i = classifyService.addClassify(classifyPojo);
            }else if(result.getStatus()=="error"){
                map.put("msg",result.getStatus()+",上传失败,数据错误");
                return map;
            }
            map.put("addSuccNum",i);
            if(i>0){
                map.put("msg", "添加成功!");
            }else{
                map.put("msg","添加失败,数据错误");
                return map;
            }
        }else{
            map.put("msg","添加失败,类型已存在");
        }
        return map;
    }

    //更新数据方法
    @RequestMapping(value = "/udpClassify",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> udpClassify(@RequestBody Classify classify){
        Map<String,Object> map = new HashMap<>();
        int exist = classifyService.findDataUnlessId(classify);
        map.put("exist", exist);
        if(exist == 0){
            int i = 0;
            int amount = classifyService.findShowCount(classify);
            int addtype = classify.getClassifyType();
            System.out.println(addtype);
            int addstate = classify.getIndexShow();
            System.out.println(addstate);
            map.put("udpSuccNum",i);
            //判断显示数据是否符合规格
            if(addtype==0 && amount>=3 && addstate==1){
                map.put("msg","修改失败,类型显示数量最多添加3个");
                return map;
            }else if(addtype==0 && amount<1 && addstate==1){
                map.put("msg","修改失败,类型显示数量最多添加1个");
                return map;
            }else if(addtype==1 && amount>=1 && addstate==1){
                map.put("msg","修改失败,文章显示数量最多添加1个");
                return map;
            }
            i = classifyService.udpClassify(classify);
            map.put("udpSuccNum",i);
            if(i>0){
                map.put("msg", "修改成功!");
            }else{
                map.put("msg","修改失败,类型不存在");
            }
        }else{
            map.put("msg","修改失败,类型已存在,或有类似类型");
        }
        return map;
    }

    //更新数据方法
    @RequestMapping(value = "/udpClassifyWithImg",method =RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> udpClassifyWithImg(@RequestParam("file") MultipartFile file,
                                          @RequestParam("classify") String classify){
        System.out.println("第1次输出"+classify);
        //将JSON数据转换成java能识别的对象类数据
        Classify classifyPojo =
                JSON.parseObject(JSON.parse(JSON.toJSONString(classify)).toString(),
                        new TypeReference<Classify>(){});
        System.out.println("第2次输出"+classify);
        System.out.println(classifyPojo.getIndexShow());
        Map<String,Object> map = new HashMap<>();
        FileUploadResult result = new FileUploadResult();
        int exist = classifyService.findDataUnlessId(classifyPojo);
        map.put("exist", exist);
        if(exist == 0){
            int i = 0;
            int amount = classifyService.findShowCount(classifyPojo);
            int addtype = classifyPojo.getClassifyType();
            int addstate = classifyPojo.getIndexShow();
            map.put("udpSuccNum",i);
            //判断显示数据是否符合规格
            if(addtype==0 && amount>=3 && addstate==1){
                map.put("msg","修改失败,类型显示数量最多添加3个");
                return map;
            }else if(addtype==0 && amount<1 && addstate==1){
                map.put("msg","修改失败,类型显示数量最多添加1个");
                return map;
            }else if(addtype==1 && amount>=1 && addstate==1){
                map.put("msg","修改失败,文章显示数量最多添加1个");
                return map;
            }
            System.out.println(classifyPojo.getClassifyImg()==null);
            System.out.println(classifyPojo.getClassifyImg().equals(""));
            System.out.println(classifyPojo.getClassifyImg()+"第1次输出");
            //如果存在图片就先删除OSS中上传的图片
            if(!classifyPojo.getClassifyImg().equals("")){
                System.out.println("进入删除OSS图片方法");
                String url = classifyPojo.getClassifyImg();
                System.out.println(url);
                url = url.replace("https://oss-container.oss-cn-beijing.aliyuncs.com/","");
                System.out.println(url);
                System.out.println(url.indexOf("?"));
                //判断路径中是否存在?
                if(url.indexOf("?")!=-1){
                    url = url.substring(0,url.indexOf("?"));
                    System.out.println(url);
                    result = ossFileManage.delete(url);
                }
            }
            System.out.println(classifyPojo.getClassifyImg()+"第2次输出");
            //如果删除成功后就再添加一个图片
            if(result.getStatus()=="removed" || classifyPojo.getClassifyImg().equals("")){
                //上传图片到OSS服务器上
                result = ossFileManage.upload(file);
                if(result.getStatus()=="done"){
                    classifyPojo.setClassifyImg(result.getName());
                    i = classifyService.udpClassify(classifyPojo);
                }else if(result.getStatus()=="error"){
                    map.put("msg",result.getStatus()+",上传失败,数据错误");
                    return map;
                }
            }else{
                map.put("msg","原图删除失败,数据错误");
                return map;
            }
            map.put("udpSuccNum",i);
            if(i>0){
                map.put("msg", "添加成功!");
            }else{
                map.put("msg","添加失败,数据错误");
                return map;
            }
        }else{
            map.put("msg","添加失败,类型已存在");
        }
        return map;
    }

    @GetMapping("/queryArticle")
    @ResponseBody
    public R articlePageQuery(){
        Classify article = classifyService.getOne(
                new QueryWrapper<Classify>().eq("classify_type", 1).eq("index_show", 1));
        return R.ok().put("article",article);
    }
}
